package com.sqstudio.util
{
	import flash.geom.Point;


	/**
	 * 
	 * @author Nestor
	 * 
	 */
	public class PointUtil
	{
		/**
		 *
		 */
		public function PointUtil()
		{
		}

		/**
		 * 计算当前点是否在给点点组成的多边形之内
		 * @param aPoint			当前点
		 * @param bPolygon			多边形顶点
		 * @return
		 */
		public static function pointInPolygon(testPoint:Array, points:Array):Boolean
		{
			var angleCount:Number = 0;
			for (var i:int=0; i < points.length; i++)
			{
				var x2:Number;
				var y2:Number;
				if (i != points.length - 1)
				{
					x2=points[i + 1][0];
					y2=points[i + 1][1];
				}
				else
				{
					x2=points[0][0];
					y2=points[0][1];
				}
				var p1:Point=new Point(points[i][0], points[i][1]);
				var p2:Point=new Point(x2, y2);
				if ((testPoint.x == p1.x && testPoint.y == p1.y) || (testPoint.x == p2.x && testPoint.y == p2.y))
				{
					angleCount=10;
					break;
				}
				angleCount+=getAngle(new Point(testPoint[0],testPoint[1]), p1, p2);
			}
			if(Math.round(angleCount) == 360){
				return true;
			}else{
				return false;
			}
		}


		/**
		 * 计算给定三点的组成三角形的夹角
		 * @param mp			夹角点
		 * @param pa			点1
		 * @param pb			点2
		 * @return 				角度值
		 */
		public static function getAngle(mp:Point, pa:Point, pb:Point):Number
		{
			var p1:Point=new Point(pa.x - mp.x, pa.y - mp.y);
			var p2:Point=new Point(pb.x - mp.x, pb.y - mp.y);
			var cosValue:Number=(p1.x * p2.x + p1.y * p2.y) / (Math.sqrt(p1.x * p1.x + p1.y * p1.y) * Math.sqrt(p2.x * p2.x + p2.y * p2.y));
			return Math.acos(cosValue) / Math.PI * 180;
		}
	}
}